<?php
namespace app\index\controller;
use app\api\controller\OssController as OSSApi;
use think\Controller;
use  app\index\model\Weixin;
use  app\index\model\Weixin_pay;
use think\Log;
use app\api\model\Poi as POI;
use app\api\model\Fareharbor as FModel;
use app\api\controller\FareGoods as F;

class Index extends Controller
{
    public function index()
    {
    	return  '前台 暂无数据  后台访问 域名/admin';
    }

     public function pay()
    {

      $userAgent = $_SERVER['HTTP_USER_AGENT']; 
    if (strpos($userAgent, 'MicroMessenger')) {

           $Weixin=new Weixin();
    
            $code=(input('param.code'));
    
          if (!empty($code)) {
             
             $res=$Weixin->get_access_token($code);
             $userinfo=$Weixin->get_openid_userinfo($res['access_token'],$res['openid']);
             $openid=$userinfo['openid'];
               // halt($openid);
    
            $Weixin_pay=new Weixin_pay(); 
            $total_fee=1;
            $body="JSAPI支付测试";
            $order_sn=time();
            $res=$Weixin_pay->pay($openid,$total_fee,$body,$order_sn);
            $this->assign("res",$res);
            return  $this->fetch("index");
    
          }else{
              
              $res=$Weixin->code_shouquan();
          }
    }else{  
      //非微信浏览器
          $Weixin_pay=new Weixin_pay(); 
         //h5支付不用传递openid 此处与微信jsapi支付不同
        $openid="";
        $total_fee=1;
        $body="wamp支付测试";
        $order_sn=time();
        $res=$Weixin_pay->payh5($openid,$total_fee,$body,$order_sn);
        // halt($res);
//        $g="https://kbx.combadc.com/admin/goods/ceshi";
//        $g=urlencode($g);
//        $url=$res['mweb_url'].'&redirect_url='.$g;
        $this->assign("res",$res);

        return  $this->fetch("h5");
    }

    	

    

        

       
    }


    public function orderquery(){
     
         $Weixin_pay=new Weixin_pay(); 
        // $openid="oZX2QwaUBh37C4Ev7I-NUdp6udao";
        $total_fee=1;
        $body="JSAPI支付测试";
        $order_sn=time();
        $res=$Weixin_pay-> orderquery();

    }

  


    public  function  notify(){
      $xml = $GLOBALS['HTTP_RAW_POST_DATA'];  
      Log::record($xml);
    }
    //批量获取poi谷歌地图缩略图
    public function getGoogleMapThumb()
    {
        ignore_user_abort();
        set_time_limit(0);
        //针对没有map_thumb_img值的POI进行图片渲染
        $list = db("poi")->field("id,lat,lng")
            ->where([
                'deleted'=>0,
                'typeid'=>1,
                'map_thumb_img'=>['eq',''],
                'lat'=>['neq',''],
                'lng'=>['neq','']
            ])
            ->limit(5)
            ->select();
        if(empty($list)){
            $this->writeLog("POI库数据已update over");
            return json(['code'=>-1,'msg'=>'POI库数据已更新完毕']);
        }
        $this->writeLog("遍历poi start ".date("Y-m-d H:i:s"));
        foreach ($list as $key=>$value){
            $id = $value['id'];
            $this->writeLog("第".$key."个POI开始请求图片,id为".$id);
            if(empty($value['lat']) || empty($value['lng'])){
                $this->writeLog("id为".$id."的POI的经纬度有空值,获取图片fail");
                continue;
            }else{
                $img = (new POI())->getGoogleMapImgByPOI($value);
                $img = json_decode($img,true);
                if(is_array($img) && isset($img['img']) && !empty($img['img'])){
                    //获取图片成功
                    $ossimgurl = (new OSSApi())->saveRemoteImgToOSS($img['img'],$id);
                    db("poi")->where("id",$id)->update(['map_thumb_img'=>$ossimgurl]);
                }else{
                    //获取图片失败
                    $this->writeLog("获取图片资源failed：".$id);
                }
            }
        }
        $this->writeLog("遍历poi end ".date("Y-m-d H:i:s"));
        return json(['code'=>200,'msg'=>'POI库OSS地图图片更新成功']);
    }
    public function getCacheData()
    {
        $post = request()->post();
        $cache_name = $post['cache'];
        if(!empty($cache_name)){
            $data = cache($cache_name);
            var_dump($data);exit;
            return $this->result($data,200,'success','json');
        }
        return 'error';
    }

    public function fareharbor()
    {
//        $cache = cache("fare_bodyglove_183");
//        $cache = cache("fare_bodyglove_183_2020-10-01");
        $cache = cache("fare_sharktourshawaii_1322_2020-09-02");
        return ($cache);
//        $company_name = 'bodyglove';
//        $result = (new F())->getFareharborData(['company'=>$company_name]);
//        return json_encode($result);
        $item = 183;
        $company = 'bodyglove';
        $currency_code = (new Fmodel())->getCompanyCurrency($company);
        $exchangeArr = db("exchange")->where("currency_code",$currency_code)->find();//汇率
        $local_exchange = $exchangeArr['exchange_rate'];//当地汇率    美元/加元
        $result = (new F())->getPackageByLastMonth($company,$item,$local_exchange,strtotime('2020-09-02'),2);
        return json_encode($result);
    }

}
